{
    "cells": [
        {
            "source": [
                "# Prerequisites\n",
                "\n",
                "### pip install K3D"
            ],
            "cell_type": "markdown",
            "metadata": {},
            "execution_count": null,
            "outputs": []
        },
        {
            "source": [
                "This ipynb file used to fail even with ipywidgets enabled.\n",
                "There was a problem with serialization of the array buffer.\n",
                "https://github.com/K3D-tools/K3D-jupyter/blob/821a59ed88579afaafababd6291e8692d70eb088/examples/camera_manipulation.ipynb\n",
                "\n",
                "# Note:\n",
                "Other K3D notebooks worked, except this, hence this test is mandatory.\n",
                "Who knows what other widgets (or same widgets under different conditions, like K3d) could fail similarly."
            ],
            "cell_type": "markdown",
            "metadata": {},
            "execution_count": null,
            "outputs": []
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# This will render a 3d image, see here for sample output.\n",
                "![k3d](https://user-images.githubusercontent.com/1948812/79030314-49dadd80-7b4d-11ea-9a39-03ddad09d119.gif)\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "metadata": {},
            "source": [
                "import k3d\n",
                "import numpy as np\n",
                "from numpy import sin,cos,pi\n",
                "from ipywidgets import interact, interactive, fixed\n",
                "import ipywidgets as widgets\n",
                "import time\n",
                "import math\n",
                "\n",
                "plot = k3d.plot()\n",
                "\n",
                "plot.camera_auto_fit = False\n",
                "\n",
                "T = 1.618033988749895\n",
                "r = 4.77\n",
                "zmin,zmax = -r,r\n",
                "xmin,xmax = -r,r\n",
                "ymin,ymax = -r,r\n",
                "Nx,Ny,Nz = 77,77,77\n",
                "\n",
                "x = np.linspace(xmin,xmax,Nx)\n",
                "y = np.linspace(ymin,ymax,Ny)\n",
                "z = np.linspace(zmin,zmax,Nz)\n",
                "x,y,z = np.meshgrid(x,y,z,indexing='ij')\n",
                "p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))\n",
                "iso = k3d.marching_cubes(p.astype(np.float32),xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax, level=0.0)\n",
                "plot += iso\n",
                "\n",
                "plot.display()"
            ]
        },
        {
            "source": [
                "# Here we expect 2 sliders to get rendered. Moving those will update the 3d image in previous cell."
            ],
            "cell_type": "markdown",
            "metadata": {},
            "execution_count": null,
            "outputs": []
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "metadata": {},
            "source": [
                "@interact(x=widgets.FloatSlider(value=0,min=-3,max=4,step=0.01))\n",
                "def g(x):\n",
                "    iso.level=x\n",
                "    \n",
                "@interact(rad=widgets.FloatSlider(value=0,min=0,max=2*math.pi,step=0.01))\n",
                "def g(rad):\n",
                "    plot.camera = [3*r*sin(rad),3*r*cos(rad),0,\n",
                "                   0,0,0,\n",
                "                   0,0,1]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": []
        }
    ],
    "metadata": {
        "celltoolbar": "Attachments",
        "file_extension": ".py",
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.7.3-final"
        },
        "mimetype": "text/x-python",
        "name": "python",
        "npconvert_exporter": "python",
        "pygments_lexer": "ipython3",
        "toc": {
            "base_numbering": 1,
            "nav_menu": {},
            "number_sections": false,
            "sideBar": false,
            "skip_h1_title": false,
            "title_cell": "Table of Contents",
            "title_sidebar": "Contents",
            "toc_cell": false,
            "toc_position": {},
            "toc_section_display": false,
            "toc_window_display": false
        },
        "version": 3
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
